// pages/pic1/pic1.js
var app = getApp()
var context = wx.createCanvasContext('canvas')
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  save: function () {
    wx.showLoading({
      title: '保存中',
      mask: true
    })
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: this.data.cwidth,
      height: this.data.cheight,
      canvasId: 'canvas',
      success: function (res) {
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (e) {
            // wx.hideLoading()
            app.showPop('保存成功', true)
          }
        })
        console.log(res.tempFilePath)
      },
      complete:function(){
        wx.hideLoading()
      }
    })
  },
  onLoad: function (options) {
    wx.showLoading({
      title: '生成中',
      mask: true
    })
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          w: res.windowWidth,
          h: res.windowHeight,
          p: res.pixelRatio,
        })
      },
    })

    wx.getImageInfo({
      src: 'https://we-fs.com/attachment/images/7/2018/09/KyWwy7XuuizJ4WwaI4aLJ4GkuL4u4U.png',
      success: function(a){
        console.log('图片信息',a)
        var context = wx.createCanvasContext('canvas')
        context.drawImage(a.path, (that.data.w * 0.9 - (that.data.w * 0.9)) / 2, 0, that.data.w * 0.9, (that.data.w * 0.9) * (a.height/a.width));

        that.setData({
          h: (that.data.w * 0.9) * (a.height / a.width)
        })
        
        context.setLineJoin('round')
        context.rect((that.data.w * 0.9 - 290) / 2, 100, 290, 290)
        
        context.setStrokeStyle('#D29826')
        context.stroke()
        context.setFillStyle('#31270C')
        context.fill()


        context.setFontSize(17)
        context.setFillStyle('#F4E49B')
        context.fillText('尊敬的' + app.globalData.pname + '老师:', (that.data.w * 0.9 - 270) / 2 + 10, 140)
        context.fillText('时光飞逝，岁月如歌；您已走', (that.data.w * 0.9 - 270) / 2 + 10 + 30, 180)
        context.fillText('过人生的' + app.globalData.page + '锦绣年华。', (that.data.w * 0.9 - 270) / 2 + 10, 220)
        context.fillText('今天是2018年9月10日，是您', (that.data.w * 0.9 - 270) / 2 + 10 + 30, 260)
        context.fillText('加入天安人寿的第' + app.globalData.pday + '天。', (that.data.w * 0.9 - 270) / 2 + 10, 300)
        context.fillText('感恩有你，与天安一路同行！', (that.data.w * 0.9 - 270) / 2 + 10 + 30, 340)
        context.draw()
        wx.hideLoading()
      }
    })
  },
  
  
})